﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VeMayBayBUS;
using VeMayBayDTO;

namespace QLVMBProject
{
    public partial class ctrBanVe : UserControl
    {
        DataGridViewComboBoxColumn clMaCB = new DataGridViewComboBoxColumn();
        DataGridViewComboBoxColumn clHangVe = new DataGridViewComboBoxColumn();
        DataGridViewTextBoxColumn clSoLuong = new DataGridViewTextBoxColumn();
        DataGridViewTextBoxColumn clDonGia = new DataGridViewTextBoxColumn();
        DataGridViewTextBoxColumn clThanhTien = new DataGridViewTextBoxColumn();
        public ctrBanVe()
        {
            InitializeComponent();


            // san bay den 
            cbSBDen.DataSource = SanBayBUS.GetSanBay();
            cbSBDen.ValueMember = "masanbay";
            cbSBDen.DisplayMember = "tensanbay";

            // san bay di
            cbSBDi.DataSource = SanBayBUS.GetSanBay();
            cbSBDi.ValueMember = "masanbay";
            cbSBDi.DisplayMember = "tensanbay";

            //coulums ma chuyen bay 
           
            clMaCB.Name = "clMaCB";
            clMaCB.HeaderText = "Mã Chuyến Bay";
            this.dgVeMB.Columns.Add(clMaCB);

            //column Hang ve   
            clHangVe.Name = "clHangVe";
            clHangVe.HeaderText = "Hạng Vé";
            this.dgVeMB.Columns.Add(clHangVe);
            //column So Luong
           
            clSoLuong.Name = "clSoLuong";
            clSoLuong.HeaderText = "Số Lượng";
            this.dgVeMB.Columns.Add(clSoLuong);
            //column Don Gia 
         
            clDonGia.Name = "clDonGia";
            clDonGia.HeaderText = "Đơn Giá";
            clDonGia.ReadOnly = true;
            this.dgVeMB.Columns.Add(clDonGia);

            //column Thanh Tien 
           
            clThanhTien.Name = "clThanhTien";
            clThanhTien.HeaderText = "Thành Tiền";
            clThanhTien.ReadOnly = true;
            this.dgVeMB.Columns.Add(clThanhTien);
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            try
            {


                ChuyenBayDTO cb = new ChuyenBayDTO();
                HanhTrinhDTO ht = new HanhTrinhDTO();
                ht.SanBayDen = (int)cbSBDen.SelectedValue;
                ht.SanBayDi = (int)cbSBDi.SelectedValue;
                DateTime datetime = new DateTime();
                datetime = dtNgay.Value;
                DataTable dt = new DataTable();
                dt = TraCuuChuyenBayBUS.TimChuyenBay(ht, datetime);

                if (dt.Rows.Count == 0)
                {
                    MessageBox.Show("Khon tim duoc cchuyen bay nao ca");
                    return;
                }
                //cl ma chuyen bay 
                clMaCB.DataSource = dt;
                clMaCB.DisplayMember = "tenmachuyenbay";
                clMaCB.ValueMember = "machuyenbay";

                // cl hang ve
                clHangVe.DataSource = HangVeBUS.GethangVe();
                clHangVe.DisplayMember = "tenhangve";
                clHangVe.ValueMember = "mahangve";

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                
                throw;
            }       
        }
        ComboBox comboBox = new ComboBox();
        TextBox textbox = new TextBox();
        private void dgVeMB_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dgVeMB.CurrentCell.ColumnIndex == dgVeMB.Columns["clHangVe"].Index && e.Control is ComboBox)
            {
                comboBox = e.Control as ComboBox;
                comboBox.SelectedIndexChanged += LastColumnComboSelectionChanged;
           
            }
            if (dgVeMB.CurrentCell.ColumnIndex == dgVeMB.Columns["clSoLuong"].Index && e.Control is TextBox)
            {
                textbox = e.Control as TextBox;
                textbox.TextChanged += LastColumnTexSelectionChanged;
            }
        }
        private void LastColumnComboSelectionChanged(object sender, EventArgs e)
        {
            try
            {
                comboBox = (ComboBox)sender;
               
                int resultIndex = -1;
                ChuyenBayDTO cb = new ChuyenBayDTO();
                cb.MaChuyenBay = (int)dgVeMB.Rows[dgVeMB.CurrentCell.RowIndex].Cells["clMaCB"].Value;
                resultIndex = (int)comboBox.SelectedValue;
                dgVeMB.Rows[dgVeMB.CurrentCell.RowIndex].Cells["clDonGia"].Value = DonGiaBUS.GetDonGiaByChuyenBay(cb,resultIndex).Rows[0]["GiaTien"];
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
                throw;
            }


            // Do saving work here
        }

        private void LastColumnTexSelectionChanged(object sender, EventArgs e)
        {
            try
            {
                textbox = (TextBox)sender;
                string soluong = textbox.Text;
                int i = 0;
                for (i = 0; i < soluong.Length; i++)
                {
                    if (char.IsDigit(soluong[i]) == false)
                    {
                        MessageBox.Show("So luong phai la so vaf lon hon hay bang 0");
                        textbox.Clear();
                    }
                }
                dgVeMB.Rows[dgVeMB.CurrentCell.RowIndex].Cells["clThanhTien"].Value = Convert.ToInt32(soluong) * (int)dgVeMB.Rows[dgVeMB.CurrentCell.RowIndex].Cells["clDonGia"].Value;





            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
                throw;
            }
            // Do saving work here
        }

        // timkiem khach hang
      

        private void btnTimKhachHang_Click(object sender, EventArgs e)
        {

            KhachHangDTO khachhang = new KhachHangDTO();
            khachhang.CMND = txtCMND.Text;
            DataTable dt = new DataTable();
            dt = KhachHangBUS.TimKiemKhachHang(khachhang);
            if (dt.Rows.Count > 0)
            {
                khachhang.TenKhachHang = dt.Rows[0]["tenKhachHang"].ToString();
                khachhang.CMND = dt.Rows[0]["cmnd"].ToString();
                khachhang.DienThoai = dt.Rows[0]["dienthoai"].ToString();
                khachhang.DiaChi = dt.Rows[0]["diachi"].ToString();
                txtHotenKH.Text = khachhang.TenKhachHang;
                txtDiaChi.Text = khachhang.DiaChi;
                txtDienThoai.Text = khachhang.DienThoai;
            }
            else
            {
                MessageBox.Show("Khong tim thay Khach Hang.\n Moi Nhap Thong Tin Khach Hang ");
            }
        }

        // btn Ok
        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                // khoi tao doi tuong
                KhachHangDTO kh = new KhachHangDTO();
                VeDTO ve = new VeDTO();
                ve.ChuyenBay = new ChuyenBayDTO();
                ve.KhachHang = new KhachHangDTO();
                ve.HangVe = new HangVeDTO();
             
                kh.CMND = txtCMND.Text;
                kh.TenKhachHang = txtHotenKH.Text;
                kh.DiaChi = txtDiaChi.Text;
                kh.DienThoai = txtDienThoai.Text;
                if (KhachHangBUS.TimKiemKhachHang(kh).Rows.Count == 0)
                {   // insert khach hang
                    KhachHangBUS.ThemKhachHang(kh);
                }
                int i=0;
                ve.KhachHang = kh;
                for (i = 0; i < dgVeMB.Rows.Count - 1; i++)
                {
                    ve.ChuyenBay.MaChuyenBay = Convert.ToInt32( dgVeMB.Rows[i].Cells["clMaCB"].Value);
                    ve.HangVe.MaHangVe = Convert.ToInt32(dgVeMB.Rows[i].Cells["clHangve"].Value);
                    ve.SoLuong = Convert.ToInt32( dgVeMB.Rows[i].Cells["clSoLuong"].Value);
                    ve.TongTien = Convert.ToInt32(dgVeMB.Rows[i].Cells["clThanhTien"].Value);
                    ve.NgayBan = DateTime.Now;
                    VeBUS.GhiNhanBanVe(ve);
                }
                MessageBox.Show("thanh cong");
                //
                dgVeMB.Rows.Clear();
                txtCMND.Clear();
                txtDiaChi.Clear();
                txtDienThoai.Clear();
                txtHotenKH.Clear();
                //
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                
                throw;
            }
            

        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Parent.Controls.Remove(this);
        }
    }
}
